﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model;
using BLL;
using System.Data;
using System.Data.SqlClient;

namespace MvcDDshop2.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/
        //登录页面
        public ActionResult Login()
        {
            if (Request.Cookies != null && Request.Cookies["UserName"] != null)
            {
                ViewBag.name = Request.Cookies["UserName"].Value;
                return View();
            }
            ViewBag.name = "";
            return View();
        }

        [HttpPost]
        public ActionResult Login(string UserName, string Pwd)
        {
            List<Login> list = LoginBLL.SelectLogin(UserName, Pwd);
            if (list.Count > 0)
            {
                HttpCookie cook = new HttpCookie("UserName", UserName);
                cook.Expires = DateTime.Now.AddMinutes(10);
                Session["userid"] = list[0].ID;
                Session["username"] = UserName;
                Session["usertype"] = list[0].UserType;
                Request.Cookies.Add(cook);
                ViewBag.name = UserName;
                if (list[0].UserType == 1)
                {
                    return RedirectToAction("Index", "Home");
                }
                else 
                {
                    return RedirectToAction("Home","Admin");
                }
            }
            else
            {
                Response.Write("<script>alert('用户名或者密码不正确');</Script>");
                return View();
            }
        }

        //注册页面
        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Register(string i )
        {
            Login l = new Model.Login();
            l.UserName = Request["UserName"];
            l.Pwd = Request["Pwd"];
            l.Email = Request["Email"];
            if (l.UserName == "")
            {
                Response.Write("<script>alert('用户名不能为空！')</script>");
                return View();
            }
            if (l.Pwd == "")
            {
                Response.Write("<script>alert('密码不能为空！')</script>");
                return View();
            }
            if (Request["Email"] == "")
            {
                Response.Write("<script>alert('邮箱不能为空！')</script>");
                return View();
            }
            if (Request["cpwd"] == "")
            {
                Response.Write("<script>alert('密码不能为空！')</script>");
                return View();
            }
            if (l.Pwd != Request["cpwd"])
            {
                Response.Write("<script>alert('两次输入的密码不同！')</script>");
                return View();
            }
            else
            {
                int num = RegisterBLL.AddLogin(l);
                return RedirectToAction("Login");
            }
           
        }
    }
}
